package com.atguigu.paymentdemo.util;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.domain.ExtUserInfo;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.atguigu.paymentdemo.mapper.OrderInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class AlipayTradeWapPay {

@Autowired
private OrderInfoMapper orderInfoMapper;
    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        // 设置商户订单号
        model.setOutTradeNo("70501111111S001111119");

        // 设置订单总金额
        model.setTotalAmount("9.00");

        // 设置订单标题
        model.setSubject("大乐透");

        // 设置产品码
        model.setProductCode("QUICK_WAP_WAY");

        // 设置针对用户授权接口
        model.setAuthToken("appopenBb64d181d0146481ab6a762c00714cC27");

        // 设置用户付款中途退出返回商户网站的地址
        model.setQuitUrl("http://www.taobao.com/product/113714.html");

        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setAlipayGoodsId("20010001");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setBody("特价手机");
        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);

        // 设置订单绝对超时时间
        model.setTimeExpire("2016-12-31 10:05:00");

        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        extendParams.setHbFqSellerPercent("100");
        extendParams.setHbFqNum("3");
        extendParams.setIndustryRefluxInfo("{\"scene_code\":\"metro_tradeorder\",\"channel\":\"xxxx\",\"scene_data\":{\"asset_name\":\"ALIPAY\"}}");
        extendParams.setSpecifiedSellerName("XXX的跨境小铺");
        extendParams.setRoyaltyFreeze("true");
        extendParams.setCardType("S0JP0000");
        model.setExtendParams(extendParams);

        // 设置商户传入业务信息
        model.setBusinessParams("{\"mc_create_trade_ip\":\"127.0.0.1\"}");

        // 设置公用回传参数
        model.setPassbackParams("merchantBizType%3d3C%26merchantBizNo%3d2016010101111");

        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");

        // 设置外部指定买家
        ExtUserInfo extUserInfo = new ExtUserInfo();
        extUserInfo.setCertType("IDENTITY_CARD");
        extUserInfo.setCertNo("362334768769238881");
        extUserInfo.setName("李明");
        extUserInfo.setMobile("16587658765");
        extUserInfo.setFixBuyer("F");
        extUserInfo.setMinAge("18");
        extUserInfo.setNeedCheckInfo("F");
        extUserInfo.setIdentityHash("27bfcd1dee4f22c8fe8a2374af9b660419d1361b1c207e9b41a754a113f38fcc");
        model.setExtUserInfo(extUserInfo);

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    public static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCiglaOJq2++N/zmKiX0/l3jsEeeLdlsSMwQv8Nv4rfyEo/v8EhcKEWqN+F7kSRRs+6qTRklGN3rSM+HXBTxvudjMumm+Ga2+O1ZzQFdCKbeb7tMgJ/A6HOvdCNvpH9uav36ORhn7HhKtrx9nDsehzo/DKqeJRXdAQUSQ7rDC+QxJQgwuQ3iXHrSIIxprWHYMjVeVQ6TuwWUAXBDw+HC6b8BNH/UasHXAI6peOJnl0fppYIj6FRgodZnyZTjkzFR4JmS5SmpjneHeQgCuqJv4VJHV7sdtsXZiwnb8RSGmv+pvLXH68MABovK0K8DiL0mAHyaVEZJOSniu+vnnywBP4nAgMBAAECggEAJDpONW5l+VbeBh4YJ5C34nFOmA5tRBxTLeUhmt0ChpTOCJwk3+GN59FJ0z+kxYY1tPUXmvyBmCJe4vAJJIQNf7fthnzltHIkt0IElNqpBGQFrjch21Lh9YbXQWIkhZ2ouqGOmDzTmNDG8n3v+qfWXhIq8VnkKALTDfqjer7Cs9Dj0OsBNQajZfqLsedZV8RaiL0GAh9n1ZQjRQOMfDvJkdyzwTY3NQ5l0gsrsu2dmnKCg7hRS+12WQ9e3Ghw+LBkYnjXppksZ+r2Get4hZQIgzLPGznrlDdZ/yQYEcNIi7zkjalU7SYGjg/yUSL1fNhilCGQR6haWZUxS5hPJaTSsQKBgQDbBMH/RGxwTV1bPxtda8Bvvat3eUceKYFrz+bFSV5IwRh1/TXg4+Xw99qv3F0JQgrZoj0nuql91k3z8EbKDvB+2Wq+3MZJoCvxiQedTxDZUAjAlqBAu2fnHS6ukdSq9OyYD0BOXnGxbRs3jbdFMKT/m1iFgSU5wKSZNAVsfDX1zQKBgQC98uq+eGPk1aPpk6AjhYk/mLSeAR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qsGSmfG3V53HQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021004193630685");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}